<style>
    .model-form .layui-form-label {
        width: 100px;
    }

    .model-form .layui-input-block {
        margin-left: 130px;
    }

    .import {
        opacity: 0;
        position: absolute;
        width: 69px;
        height: 38px;
        margin-left: -20px;
        cursor: pointer;
    }

    xm-select > .xm-body .scroll-body {
        max-height: 220px;
    }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">部门名称：</label>
                                    <div class="layui-input-inline">
                                        <input name="departmentId" type="hidden"/>
                                        <div class="layui-input-block" style="width: 190px; margin-left: 0px;"
                                             id="queryDepartment">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog"
                                            lay-submit layui-form-keyDownSearch="true">查询
                                    </button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt" style="margin-top: 10px;">
                            <button id="departmentSyncBtn" class="layui-btn icon-btn color-reseda">部门同步</button>
                            <button id="userSyncBtn" class="layui-btn icon-btn color-reseda">成员同步</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="departmentTable" lay-filter="departmentTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="departmentTableBar">
    <a class="layui-btn layui-btn-list layui-btn-xs" lay-event="userList">成员列表</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="departmentForm">
    <form lay-filter="departmentForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item" id = "parentDepartmentDiv">
            <label class="layui-form-label">上级部门</label>
            <input name="parentid" type="hidden"/>
            <div class="layui-input-block" id="selectDepartment">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入名称" type="text" class="layui-input" maxlength="200" lay-verify="required" required autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">英文名称</label>
            <div class="layui-input-block">
                <input name="name_en" placeholder="请输入英文名称" type="text" class="layui-input" maxlength="200"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序值</label>
            <div class="layui-input-block">
                <input name="order" placeholder="请输入排序值" type="text" class="layui-input" maxlength="200"/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="departmentFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- 部门成员表单弹窗 -->
<script type="text/html" id="departmentUserListForm">
    <div class="layui-form-item" style="width: 98%; margin-left: 10px; margin-top: -10px;">
        <table class="layui-table" id="departmentUserListTable" lay-filter="departmentUserListTable"></table>
    </div>
</script>

<!-- 部门成员修改弹窗 -->
<script type="text/html" id="departmentUserEditForm">
    <form lay-filter="departmentUserEditForm" class="layui-form model-form">
        <input name="departmentId" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">用户ID</label>
            <div class="layui-input-block">
                <input name="userid" placeholder="请输入用户ID" type="text" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入名称" type="text" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号码</label>
            <div class="layui-input-block">
                    <input name="mobile" placeholder="请输入手机号码" type="text" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">职务信息</label>
            <div class="layui-input-block">
                <input name="position" placeholder="请输入职务信息" type="text" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input name="email" placeholder="请输入邮箱" type="text" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="departmentFormEditSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- 表格操作列 -->
<script type="text/html" id="departmentUserListTableBar">
    <a class="layui-btn layui-btn-list layui-btn-xs" lay-event="scheduleList">排班信息</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="userEdit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="userDel">删除</a>
</script>

<!-- 成员排班弹窗 -->
<script type="text/html" id="scheduleListForm">
    <form lay-filter="scheduleListForm" class="layui-form model-form">
        <div class="layui-col-xs4">
            <label class="layui-form-label">用户</label>
            <div class="layui-input-block">
                <input name="userid" type="text" class="layui-input"/>
            </div>
        </div>
        <div class="layui-col-xs4">
            <label class="layui-form-label">排班月份</label>
            <div class="layui-input-block">
                <input name="yearmonth" type="text" class="layui-input"/>
            </div>
        </div>
        <div class="layui-col-xs4">
            <label class="layui-form-label">打卡规则名</label>
            <div class="layui-input-block">
                <input name="groupname" type="text" class="layui-input"/>
            </div>
        </div>
    </form>
    <div class="layui-form-item" style="width: 98%; margin-left: 10px; margin-top: -10px;">
        <table class="layui-table" id="scheduleListTable" lay-filter="scheduleListTable"></table>
    </div>
</script>

<!-- js部分 -->
<script type="text/javascript" src="../../../admin/assets/js/jsencrypt.min.js"></script>
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'tableTreeDj'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var tableTree = layui.tableTreeDj;

        //部门
        admin.initDataSelectTree(true, "production/productionSchedule/admin/productionSchedule/department/getTreeData", null, "queryDepartment", null, "部门", "departmentId", null);

        // 渲染表格
        var insTb = tableTree.render({
            id: '#departmentTable',
            elem: '#departmentTable',
            url: config.base + 'production/productionSchedule/admin/productionSchedule/department/list',
            page: false,
            cellMinWidth: 120,
            cols: [[
                {type: 'numbers', title: '序号'},
                {field: 'id', align: 'center', title: 'ID', width: 100, hide: true},
                {field: 'name', align: 'center', title: '部门名称'},
                {field: 'name_en', align: 'center', title: '英文名称'},
                {field: 'order', align: 'center', title: '排序值'},
                {align: 'center', toolbar: '#departmentTableBar', title: '操作', width: 200}
            ]]
        }, {keyPid: "parentid", title: "name"});

        // 工具条点击事件
        table.on('tool(departmentTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'userList') { //成员列表
                showDepartmentUserListModel(data);
            } else if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(departmentTable)', function (obj) {
            table.reload('departmentTable', {
                initSort: obj,
                where: {
                    sort: obj.field,
                    order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field}, 'data');
        });

        // 重置搜索
        form.on('submit(resets)', data => {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field}, 'data');
        });

        // 部门同步事件
        $('#departmentSyncBtn').click(function () {
            console.info("部门同步");
        });

        // 成员同步事件
        $('#userSyncBtn').click(function () {
            console.info("成员同步");
        });

        //显示成员列表弹窗
        function showDepartmentUserListModel(data) {
            admin.open({
                type: 1,
                area: ['80%', '80%'],
                offset: ['125px', '300px'],
                title: '部门成员列表',
                content: $('#departmentUserListForm').html(),
                success: function () {
                    table.render({
                        id: '#departmentUserListTable',
                        elem: '#departmentUserListTable',
                        url: config.base + 'production/productionSchedule/admin/productionSchedule/user/list?departmentId=' + data.id,
                        page: false,
                        cellMinWidth: 120,
                        cols: [[
                            {type: 'numbers', title: '序号', width: 80},
                            {field: 'userid', align: 'center', title: '用户ID'},
                            {field: 'name', align: 'center', title: '名称'},
                            {field: 'mobile', align: 'center', title: '手机号码'},
                            {field: 'position', align: 'center', title: '职务信息'},
                            {
                                field: 'gender', align: 'center', title: '性别', width: 150,
                                templet: function (d) {
                                    if (d.gender == 0) {
                                        return "未定义"
                                    } else if (d.gender == 1) {
                                        return "男"
                                    } else if (d.gender == 2) {
                                        return "女"
                                    }
                                }
                            },
                            {field: 'email', align: 'center', title: '邮箱'},
                            {align: 'center', toolbar: '#departmentUserListTableBar', title: '操作', width: 200}
                        ]]
                    });
                }
            });
        }

        //成员排班列表弹窗
        function scheduleListModel(data) {
            admin.open({
                type: 1,
                area: ['80%', '80%'],
                offset: ['125px', '300px'],
                title: '成员排班列表',
                content: $('#scheduleListForm').html(),
                success: function () {
                    table.render({
                        id: '#scheduleListTable',
                        elem: '#scheduleListTable',
                        url: config.base + 'production/productionSchedule/admin/productionSchedule/schedule/list?userid='+data.userid,
                        page: false,
                        cellMinWidth: 120,
                        cols: [[
                            {type: 'numbers', title: '序号', width: 80},
                            {field: 'userid', align: 'center', title: '打卡人员'},
                            {field: 'yearmonth', align: 'center', title: '打卡人员'},
                            {field: 'groupname', align: 'center', title: '打卡规则名'}
                        ]]
                    });
                }
            });
        }

        // 工具条点击事件
        table.on('tool(departmentUserListTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'scheduleList') {  //排班信息
                scheduleListModel(data);
            } else if (obj.event === 'userEdit') { //修改
                userEditModel(data);
            } else if (obj.event === 'userDel') { // 删除
                userDelete(obj);
            }
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '720px',
                offset: ['165px','665px'],
                title: '修改部门信息',
                content: $('#departmentForm').html(),
                success: function () {
                    if(data.parentid == 0){
                        data.parentid = null;
                        $("#parentDepartmentDiv").hide();
                    }else{
                        //上级部门
                        admin.initDataSelectTree(true, "production/productionSchedule/admin/productionSchedule/department/getTreeData", null, "selectDepartment", data.parentid, "上级部门", "parentid", null);
                    }
                    //处理中文名称
                    var patt1 = /[\u4e00-\u9fa5]+/g;
                    var letters = data.name.match(patt1).join("");
                    data.name =  letters;
                    //数据回显
                    form.val('departmentForm', data);
                    // 表单提交事件
                    form.on('submit(departmentFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req("production/productionSchedule/admin/productionSchedule/department/update", d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload('departmentTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除部门吗？', {
                offset: ['200px','45%'],
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('production/productionSchedule/admin/productionSchedule/department/delete', {departmentId: obj.data.id}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'GET');
            });
        }

        // 部门成员编辑弹窗
        function userEditModel(data) {
            var departmentUserEditIndex = admin.open({
                type: 1,
                area: '720px',
                offset: ['165px', '665px'],
                title: '编辑成员信息',
                content: $('#departmentUserEditForm').html(),
                success: function () {
                    //数据回显
                    form.val('departmentUserEditForm', data);
                    // 表单提交事件
                    form.on('submit(departmentFormEditSubmit)', function (d) {
                        layer.load(2);
                        admin.req("production/productionSchedule/admin/productionSchedule/user/update", d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('#departmentUserListTable');
                                layer.close(departmentUserEditIndex);
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function userDelete(obj) {
            layer.confirm('确定要删除部门成员吗？', {
                offset: ['200px','45%'],
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('production/productionSchedule/admin/productionSchedule/user/delete', {userId: obj.data.userid}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'GET');
            });
        }


    });
</script>